﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using YDWin.Base;
using YDServices;
using YDModel;
using DevExpress.Utils;
using YDCommon;
using System.Security.Cryptography;

namespace YDWin.Employee
{
    public partial class AddEmployeeForm : AddForm
    {
        EmployeeService svr = new EmployeeService();
        public T_Employee editTu = new T_Employee();
        public string state = "";
        public AddEmployeeForm()
        {
            InitializeComponent();
 
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
          
            T_Employee fact = new T_Employee();
            if (!state.Equals("") && state.Equals("Edit"))
            {
                fact = editTu;
            }

            if (txtEmployeeName.Text.Trim().Equals(""))
            {
                Utils.ShowPrompt(MessageType.mtWarning, "员工姓名不能为空!\n");  
                return;
            }

            //验证2次输入的密码是否一致
            if (!txtUserPassword.Text.Trim().Equals(txtUserPassword2.Text.Trim()))
            {
                Utils.ShowPrompt(MessageType.mtWarning, "两次输入的密码不一致,请重新输入!\n");  
                return;
            }

            fact.EmployeeName = txtEmployeeName.Text.Trim();
            fact.UserCode = txtUserCode.Text.Trim(); 
            fact.UserPassword = Utils.Encrypt(txtUserPassword.Text.Trim());
            WaitDialogForm sdf = new WaitDialogForm("", "正在保存......");
            if (!state.Equals("") && state.Equals("Edit"))
            {
                svr.EditItem(fact,Global.User);
            }
            else
            {
                svr.AddItem(fact, Global.User);
            }
      

            sdf.Close();

            if (svr.HasError)
            {
                Utils.ShowPrompt(MessageType.mtError, "员工信息保存失败：" + svr.Errors);
                svr.Errors.Clear();
                return;
            }

            this.DialogResult = DialogResult.OK;
            this.Close();

        }

        private void AddEmployeeForm_Load(object sender, EventArgs e)
        {
            if (!state.Equals("") && state.Equals("Edit"))
            {
                txtEmployeeName.Text = editTu.EmployeeName;
                txtUserCode.Text = editTu.UserCode;
                txtUserPassword2.ReadOnly = true;
                txtUserPassword.ReadOnly = true;
                txtUserPassword.Text = editTu.UserPassword;
                txtUserPassword2.Text = editTu.UserPassword;
            }
        }
 
    }
}